/// 用户数据统计
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../model/my_store.dart'; // 本地存储封装
import '../../model/config.dart'; // 全局配置文件
class Statistics extends StatefulWidget {
  Statistics({
    Key key,
    this.csid
  }):super(key: key);
  int csid;
  _StatisticsState createState() => _StatisticsState();
}

class _StatisticsState extends State<Statistics> with AutomaticKeepAliveClientMixin {
  List userList; // 客服信息
  @override
  void initState () {
    userList = [];
    getUserList();
    super.initState();
  }
  void getUserList () {
    // 获取用户消息列表
    var LocaUserList = MyStore().getData('ALLUSER${widget.csid}');
    var MsgUserList = LocaUserList != null ? LocaUserList['list'] : null;
    if (MsgUserList == null) {
      // 本地存储没有消息列表，从服务器获取
      Dio().get(
        MyConfig().baseurl + '/csuser/getalluser',
        data: {
          'butt_id' : widget.csid
        }
      ).then((response) {
        if (response.statusCode == 200 && response.data['status'] == 1) {
          setState(() {userList = [];});
          response.data['msg'].forEach((item) { setState(() { userList.add(item); }); });
          MyStore().setData('ALLUSER${widget.csid}', {
            'list': userList
          });
        } else {
          setState(() {userList = [];});
        }
      }).catchError((err) {
        setState(() {userList = [];});
      });
    } else {
      setState(() {userList = MsgUserList;});
    }
  }
  @override

  

  Widget build(BuildContext context) {
    return Container(
      color: Colors.black12,
      child: ListView(
        children: <Widget>[
          Container(
            margin: EdgeInsets.all(ScreenUtil().setWidth(15)),
            height: ScreenUtil().setWidth(150),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5),
              boxShadow: [
                BoxShadow(
                  color: Colors.black12,
                  offset: Offset(0.0, 4),
                  blurRadius: 5.0
                )
              ]
            ),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Container(
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '已沟通',
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil().setWidth(30),
                            ),
                          ),
                          Text(
                            '100',
                            style: TextStyle(
                              fontSize: ScreenUtil().setWidth(40),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '未沟通',
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil().setWidth(30),
                            ),
                          ),
                          Text(
                            '52',
                            style: TextStyle(
                              fontSize: ScreenUtil().setWidth(40),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '取 关',
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil().setWidth(30),
                            ),
                          ),
                          Text(
                            '84',
                            style: TextStyle(
                              fontSize: ScreenUtil().setWidth(40),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Center(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '取关率',
                            style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil().setWidth(30),
                            ),
                          ),
                          Text(
                            '52.31%',
                            style: TextStyle(
                              fontSize: ScreenUtil().setWidth(40),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
  @protected
  bool get wantKeepAlive => true;
}